package com.lzp.controller;

import com.lzp.entity.Student;
import com.lzp.service.IStudentService;
import com.lzp.util.PageResult;
import com.lzp.util.R;
import com.lzp.vo.StudentVO;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 李智鹏
 * @since 2025-01-07
 */
@RestController
@RequestMapping("/student")
public class StudentController {
    @Autowired
    private IStudentService studentService;
    //学生列表
    @RequestMapping("/list")
    public R getStudent(){
        return R.ok().data("list",studentService.list());
    }

    @GetMapping("/findByPage/{page}/{pageSize}")//分页查询
    public R findByPage(@PathVariable Integer page, @PathVariable Integer pageSize){
        PageResult<Student> pr=studentService.findByPage(page,pageSize);
        return R.ok().data("pr",pr);
    }

    //条件查询带分页
    @RequestMapping("/search/{page}/{pageSize}")
    public R search(@PathVariable("page") Integer page,
                    @PathVariable("pageSize") Integer pageSize,
                    @RequestBody(required = false) StudentVO vo){
        PageResult<Student> pr=studentService.search(page,pageSize,vo);
        return R.ok().data("pr",pr);
    }

    @PostMapping("/add")
    public R add(@RequestBody Student student){
        //studentService.add(student);//添加学生
        //return R.ok().message("学生添加成功");
        boolean flag=studentService.add(student);
        return flag?R.ok().message("学生添加成功"):R.error().message("学生添加失败");
    }
    @PutMapping("/update")//修改学生
    public R update(@RequestBody Student student){
        //studentService.updateById(student);
        //return R.ok().message("学生修改成功");
        boolean flag=studentService.updateById(student);
        return flag?R.ok().message("学生修改成功"):R.error().message("学生修改失败");
    }

    //删除学生
    @DeleteMapping("/delete/{ids}")
    public R delete(@PathVariable("ids") String ids){
        String[] split=ids.split(",");//分割,
        List<String> sids=Arrays.asList(split);
        return studentService.removeByIds(sids)?R.ok().message("删除学生成功!"):R.error().message("删除成功");
    }
}






































